﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace WyUnity.Utility
{
    /// <summary>
    /// 序列化工具
    /// </summary>
    public static class Serialiaze
    {
        /// <summary>
        /// 对象序列化成二进制数组
        /// </summary>
        /// <param name="obj">要序列化对象</param>
        /// <returns></returns>
        public static byte[] ObjectToBytes(object obj)
        {
            using (var ms = new MemoryStream())
            {

                var b = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                b.Serialize(ms, obj);
                
                return ms.ToArray();
            }
        }

        /// <summary>
        /// 从二进制数组还原对象
        /// </summary>
        /// <param name="obj">对象的二进制格式数据</param>
        /// <returns></returns>
        public static object BytesToObject(byte[] obj)
        {
            using (var ms = new MemoryStream(obj))
            {
                var b = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                return b.Deserialize(ms);
            }

        }

        /// <summary>
        /// 序列化对象为xml字符串
        /// </summary>
        /// <param name="obj">要序列化的对象</param>
        /// <returns></returns>
        public static string ObjectToXml(object obj)
        {
              var xml = new System.Xml.Serialization.XmlSerializer(obj.GetType());

                using (var ms = new MemoryStream())
                {
                    xml.Serialize(ms, obj);
                    return Encoding.UTF8.GetString(ms.ToArray());
                }


        }

        /// <summary>
        /// 从Xml字符串还原对象
        /// </summary>
        /// <param name="xmlString">对象的Xml格式字符串</param>
        /// <param name="type">对象类型</param>
        /// <returns></returns>
        public static object XmlToObject(string xmlString, Type type)
        {

            var xml = new System.Xml.Serialization.XmlSerializer(type);

            using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(xmlString)))
            {
                ms.Position = 0;
                return xml.Deserialize(ms);
            }
        }

        /// <summary>
        /// 序列化对象为json字符串
        /// </summary>
        /// <param name="obj">要序列化的对象</param>
        /// <returns></returns>
        public static string ObjectToJson(object obj)
        {
            return Newtonsoft.Json.JsonConvert.SerializeObject(obj);
        }

        /// <summary>
        /// 从json字符串还原对象
        /// </summary>
        /// <param name="jsonString">对象的json格式字符串</param>
        /// <param name="type">对象类型</param>
        /// <returns></returns>
        public static object JsonToObject(string jsonString, Type type)
        {
            try
            {
                return Newtonsoft.Json.JsonConvert.DeserializeObject(jsonString, type);
            }
            catch
            {
                Log.Get("WyUnity2").Info("对象反序列化发生异常");
                return null;
            }
        }
    }
}
